package gui;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class studentinfo {
    public  studentinfo() {
        
            JFrame f = new JFrame("学生信息管理");
            f.setSize(400, 400);
            f.setLocationRelativeTo(null);
            Container con = f.getContentPane();

            String[] colName = {"学号", "姓名", "性别", "出生年份"};

            DefaultTableModel model = new DefaultTableModel(new Object[0][],colName);
            JTable table = new JTable(model);
           

            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewportView(table);
            con.setLayout(new GridLayout(2, 1));

            // 信息部分
            JPanel pan1 = new JPanel();
            pan1.setLayout(null);
            JLabel num = new JLabel("学号");
            TextField uid = new TextField(11);
            num.setBounds(50, 20, 100, 20);
            uid.setBounds(150, 20, 100, 20);
            pan1.add(num);
            pan1.add(uid);

            JLabel name = new JLabel("姓名");
            TextField uname = new TextField(11);
            name.setBounds(50, 50, 100, 20);
            uname.setBounds(150, 50, 100, 20);
            pan1.add(name);
            pan1.add(uname);

            JLabel sex = new JLabel("性别");
            ButtonGroup groupsex = new ButtonGroup();
            JRadioButton man = new JRadioButton("男", true);
            JRadioButton woman = new JRadioButton("女");
            sex.setBounds(50, 80, 100, 20);
            man.setBounds(150, 80, 60, 20);
            woman.setBounds(250, 80, 60, 20);
            groupsex.add(man);
            groupsex.add(woman);
            pan1.add(sex);
            pan1.add(man);
            pan1.add(woman);

            JLabel age = new JLabel("出生年份");
            String[] items = {"2000", "2001", "2002"};
            JComboBox<String> comboBox = new JComboBox<>(items);
            age.setBounds(50, 110, 100, 20);
            comboBox.setBounds(150, 110, 100, 20);
            pan1.add(age);
            pan1.add(comboBox);

            JButton button1 = new JButton("添加");
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String sex = null;
                    String stuid = null;
                    String stuname = null;
                    if (man.isSelected()&&!uid.getText().isEmpty()&& !uname.getText().isEmpty()) {
                        sex = "男";
                        stuid = uid.getText();
                        stuname = uname.getText();
                    } else if(woman.isSelected()&&!uid.getText().isEmpty()&& !uname.getText().isEmpty()) {
                        sex = "女";
                        stuid = uid.getText();
                        stuname = uname.getText();
                    }else {
                        // 处理错误情况，如未选择性别或未输入学号和姓名
                        JOptionPane.showMessageDialog(f, "请完整填写学生信息", "错误", JOptionPane.ERROR_MESSAGE);
                        return;}

                    String[] stuinfo = {stuid,stuname, sex, comboBox.getSelectedItem().toString()};
                    model.addRow(stuinfo);
                    uid.setText("");
                    uname.setText("");
                    man.setSelected(true);
                }
            });
            JButton button2 = new JButton("删除");
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = table.getSelectedRow();
                    if (selectedRow != -1) {
                        model.removeRow(selectedRow);
                    } else {
                        JOptionPane.showMessageDialog(f, "请选择要删除的行", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            JButton button3 = new JButton("退出");
            button3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    f.dispose();
                }
            });
            button1.setBounds(80, 160, 60, 20);
            button2.setBounds(150, 160, 60, 20);
            button3.setBounds(220, 160, 60, 20);
            pan1.add(button1);
            pan1.add(button2);
            pan1.add(button3);
            con.add(pan1);
            con.add(scrollPane);

            f.setVisible(true);
       
    }
}
